{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>6.7</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.3</td>\n",
       "      <td>Iris-virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>6.3</td>\n",
       "      <td>2.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.9</td>\n",
       "      <td>Iris-virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>6.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Iris-virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>6.2</td>\n",
       "      <td>3.4</td>\n",
       "      <td>5.4</td>\n",
       "      <td>2.3</td>\n",
       "      <td>Iris-virginica</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>5.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.1</td>\n",
       "      <td>1.8</td>\n",
       "      <td>Iris-virginica</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       0    1    2    3               4\n",
       "145  6.7  3.0  5.2  2.3  Iris-virginica\n",
       "146  6.3  2.5  5.0  1.9  Iris-virginica\n",
       "147  6.5  3.0  5.2  2.0  Iris-virginica\n",
       "148  6.2  3.4  5.4  2.3  Iris-virginica\n",
       "149  5.9  3.0  5.1  1.8  Iris-virginica"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.read_csv(\"iris.data\", header=None)\n",
    "df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "source": [
    "a = df.iloc[[0]]\n",
    "b = df.iloc[0:1]\n",
    "c = df.iloc[0]\n",
    "\n",
    "print(type(a))\n",
    "print(type(b))\n",
    "print(type(c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.1, 3.5, 1.4, 0.2, 1. ],\n",
       "       [4.9, 3. , 1.4, 0.2, 1. ],\n",
       "       [4.7, 3.2, 1.3, 0.2, 1. ],\n",
       "       [4.6, 3.1, 1.5, 0.2, 1. ],\n",
       "       [5. , 3.6, 1.4, 0.2, 1. ],\n",
       "       [5.4, 3.9, 1.7, 0.4, 1. ],\n",
       "       [4.6, 3.4, 1.4, 0.3, 1. ],\n",
       "       [5. , 3.4, 1.5, 0.2, 1. ],\n",
       "       [4.4, 2.9, 1.4, 0.2, 1. ],\n",
       "       [4.9, 3.1, 1.5, 0.1, 1. ],\n",
       "       [5.4, 3.7, 1.5, 0.2, 1. ],\n",
       "       [4.8, 3.4, 1.6, 0.2, 1. ],\n",
       "       [4.8, 3. , 1.4, 0.1, 1. ],\n",
       "       [4.3, 3. , 1.1, 0.1, 1. ],\n",
       "       [5.8, 4. , 1.2, 0.2, 1. ],\n",
       "       [5.7, 4.4, 1.5, 0.4, 1. ],\n",
       "       [5.4, 3.9, 1.3, 0.4, 1. ],\n",
       "       [5.1, 3.5, 1.4, 0.3, 1. ],\n",
       "       [5.7, 3.8, 1.7, 0.3, 1. ],\n",
       "       [5.1, 3.8, 1.5, 0.3, 1. ],\n",
       "       [5.4, 3.4, 1.7, 0.2, 1. ],\n",
       "       [5.1, 3.7, 1.5, 0.4, 1. ],\n",
       "       [4.6, 3.6, 1. , 0.2, 1. ],\n",
       "       [5.1, 3.3, 1.7, 0.5, 1. ],\n",
       "       [4.8, 3.4, 1.9, 0.2, 1. ],\n",
       "       [5. , 3. , 1.6, 0.2, 1. ],\n",
       "       [5. , 3.4, 1.6, 0.4, 1. ],\n",
       "       [5.2, 3.5, 1.5, 0.2, 1. ],\n",
       "       [5.2, 3.4, 1.4, 0.2, 1. ],\n",
       "       [4.7, 3.2, 1.6, 0.2, 1. ],\n",
       "       [4.8, 3.1, 1.6, 0.2, 1. ],\n",
       "       [5.4, 3.4, 1.5, 0.4, 1. ],\n",
       "       [5.2, 4.1, 1.5, 0.1, 1. ],\n",
       "       [5.5, 4.2, 1.4, 0.2, 1. ],\n",
       "       [4.9, 3.1, 1.5, 0.1, 1. ],\n",
       "       [5. , 3.2, 1.2, 0.2, 1. ],\n",
       "       [5.5, 3.5, 1.3, 0.2, 1. ],\n",
       "       [4.9, 3.1, 1.5, 0.1, 1. ],\n",
       "       [4.4, 3. , 1.3, 0.2, 1. ],\n",
       "       [5.1, 3.4, 1.5, 0.2, 1. ],\n",
       "       [5. , 3.5, 1.3, 0.3, 1. ],\n",
       "       [4.5, 2.3, 1.3, 0.3, 1. ],\n",
       "       [4.4, 3.2, 1.3, 0.2, 1. ],\n",
       "       [5. , 3.5, 1.6, 0.6, 1. ],\n",
       "       [5.1, 3.8, 1.9, 0.4, 1. ],\n",
       "       [4.8, 3. , 1.4, 0.3, 1. ],\n",
       "       [5.1, 3.8, 1.6, 0.2, 1. ],\n",
       "       [4.6, 3.2, 1.4, 0.2, 1. ],\n",
       "       [5.3, 3.7, 1.5, 0.2, 1. ],\n",
       "       [5. , 3.3, 1.4, 0.2, 1. ],\n",
       "       [7. , 3.2, 4.7, 1.4, 1. ],\n",
       "       [6.4, 3.2, 4.5, 1.5, 1. ],\n",
       "       [6.9, 3.1, 4.9, 1.5, 1. ],\n",
       "       [5.5, 2.3, 4. , 1.3, 1. ],\n",
       "       [6.5, 2.8, 4.6, 1.5, 1. ],\n",
       "       [5.7, 2.8, 4.5, 1.3, 1. ],\n",
       "       [6.3, 3.3, 4.7, 1.6, 1. ],\n",
       "       [4.9, 2.4, 3.3, 1. , 1. ],\n",
       "       [6.6, 2.9, 4.6, 1.3, 1. ],\n",
       "       [5.2, 2.7, 3.9, 1.4, 1. ],\n",
       "       [5. , 2. , 3.5, 1. , 1. ],\n",
       "       [5.9, 3. , 4.2, 1.5, 1. ],\n",
       "       [6. , 2.2, 4. , 1. , 1. ],\n",
       "       [6.1, 2.9, 4.7, 1.4, 1. ],\n",
       "       [5.6, 2.9, 3.6, 1.3, 1. ],\n",
       "       [6.7, 3.1, 4.4, 1.4, 1. ],\n",
       "       [5.6, 3. , 4.5, 1.5, 1. ],\n",
       "       [5.8, 2.7, 4.1, 1. , 1. ],\n",
       "       [6.2, 2.2, 4.5, 1.5, 1. ],\n",
       "       [5.6, 2.5, 3.9, 1.1, 1. ],\n",
       "       [5.9, 3.2, 4.8, 1.8, 1. ],\n",
       "       [6.1, 2.8, 4. , 1.3, 1. ],\n",
       "       [6.3, 2.5, 4.9, 1.5, 1. ],\n",
       "       [6.1, 2.8, 4.7, 1.2, 1. ],\n",
       "       [6.4, 2.9, 4.3, 1.3, 1. ],\n",
       "       [6.6, 3. , 4.4, 1.4, 1. ],\n",
       "       [6.8, 2.8, 4.8, 1.4, 1. ],\n",
       "       [6.7, 3. , 5. , 1.7, 1. ],\n",
       "       [6. , 2.9, 4.5, 1.5, 1. ],\n",
       "       [5.7, 2.6, 3.5, 1. , 1. ],\n",
       "       [5.5, 2.4, 3.8, 1.1, 1. ],\n",
       "       [5.5, 2.4, 3.7, 1. , 1. ],\n",
       "       [5.8, 2.7, 3.9, 1.2, 1. ],\n",
       "       [6. , 2.7, 5.1, 1.6, 1. ],\n",
       "       [5.4, 3. , 4.5, 1.5, 1. ],\n",
       "       [6. , 3.4, 4.5, 1.6, 1. ],\n",
       "       [6.7, 3.1, 4.7, 1.5, 1. ],\n",
       "       [6.3, 2.3, 4.4, 1.3, 1. ],\n",
       "       [5.6, 3. , 4.1, 1.3, 1. ],\n",
       "       [5.5, 2.5, 4. , 1.3, 1. ],\n",
       "       [5.5, 2.6, 4.4, 1.2, 1. ],\n",
       "       [6.1, 3. , 4.6, 1.4, 1. ],\n",
       "       [5.8, 2.6, 4. , 1.2, 1. ],\n",
       "       [5. , 2.3, 3.3, 1. , 1. ],\n",
       "       [5.6, 2.7, 4.2, 1.3, 1. ],\n",
       "       [5.7, 3. , 4.2, 1.2, 1. ],\n",
       "       [5.7, 2.9, 4.2, 1.3, 1. ],\n",
       "       [6.2, 2.9, 4.3, 1.3, 1. ],\n",
       "       [5.1, 2.5, 3. , 1.1, 1. ],\n",
       "       [5.7, 2.8, 4.1, 1.3, 1. ],\n",
       "       [6.3, 3.3, 6. , 2.5, 1. ],\n",
       "       [5.8, 2.7, 5.1, 1.9, 1. ],\n",
       "       [7.1, 3. , 5.9, 2.1, 1. ],\n",
       "       [6.3, 2.9, 5.6, 1.8, 1. ],\n",
       "       [6.5, 3. , 5.8, 2.2, 1. ],\n",
       "       [7.6, 3. , 6.6, 2.1, 1. ],\n",
       "       [4.9, 2.5, 4.5, 1.7, 1. ],\n",
       "       [7.3, 2.9, 6.3, 1.8, 1. ],\n",
       "       [6.7, 2.5, 5.8, 1.8, 1. ],\n",
       "       [7.2, 3.6, 6.1, 2.5, 1. ],\n",
       "       [6.5, 3.2, 5.1, 2. , 1. ],\n",
       "       [6.4, 2.7, 5.3, 1.9, 1. ],\n",
       "       [6.8, 3. , 5.5, 2.1, 1. ],\n",
       "       [5.7, 2.5, 5. , 2. , 1. ],\n",
       "       [5.8, 2.8, 5.1, 2.4, 1. ],\n",
       "       [6.4, 3.2, 5.3, 2.3, 1. ],\n",
       "       [6.5, 3. , 5.5, 1.8, 1. ],\n",
       "       [7.7, 3.8, 6.7, 2.2, 1. ],\n",
       "       [7.7, 2.6, 6.9, 2.3, 1. ],\n",
       "       [6. , 2.2, 5. , 1.5, 1. ],\n",
       "       [6.9, 3.2, 5.7, 2.3, 1. ],\n",
       "       [5.6, 2.8, 4.9, 2. , 1. ],\n",
       "       [7.7, 2.8, 6.7, 2. , 1. ],\n",
       "       [6.3, 2.7, 4.9, 1.8, 1. ],\n",
       "       [6.7, 3.3, 5.7, 2.1, 1. ],\n",
       "       [7.2, 3.2, 6. , 1.8, 1. ],\n",
       "       [6.2, 2.8, 4.8, 1.8, 1. ],\n",
       "       [6.1, 3. , 4.9, 1.8, 1. ],\n",
       "       [6.4, 2.8, 5.6, 2.1, 1. ],\n",
       "       [7.2, 3. , 5.8, 1.6, 1. ],\n",
       "       [7.4, 2.8, 6.1, 1.9, 1. ],\n",
       "       [7.9, 3.8, 6.4, 2. , 1. ],\n",
       "       [6.4, 2.8, 5.6, 2.2, 1. ],\n",
       "       [6.3, 2.8, 5.1, 1.5, 1. ],\n",
       "       [6.1, 2.6, 5.6, 1.4, 1. ],\n",
       "       [7.7, 3. , 6.1, 2.3, 1. ],\n",
       "       [6.3, 3.4, 5.6, 2.4, 1. ],\n",
       "       [6.4, 3.1, 5.5, 1.8, 1. ],\n",
       "       [6. , 3. , 4.8, 1.8, 1. ],\n",
       "       [6.9, 3.1, 5.4, 2.1, 1. ],\n",
       "       [6.7, 3.1, 5.6, 2.4, 1. ],\n",
       "       [6.9, 3.1, 5.1, 2.3, 1. ],\n",
       "       [5.8, 2.7, 5.1, 1.9, 1. ],\n",
       "       [6.8, 3.2, 5.9, 2.3, 1. ],\n",
       "       [6.7, 3.3, 5.7, 2.5, 1. ],\n",
       "       [6.7, 3. , 5.2, 2.3, 1. ],\n",
       "       [6.3, 2.5, 5. , 1.9, 1. ],\n",
       "       [6.5, 3. , 5.2, 2. , 1. ],\n",
       "       [6.2, 3.4, 5.4, 2.3, 1. ],\n",
       "       [5.9, 3. , 5.1, 1.8, 1. ]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = df.iloc[:, 4]\n",
    "X = df.iloc[:, 0:4].values\n",
    "ones = np.ones([len(X), 1])\n",
    "X = np.column_stack((X, ones))\n",
    "X\n",
    "# y = y.reshape([-1, 1])\n",
    "# y = np.where(y=='Iris-versicolor', -1, 1)\n",
    "\n",
    "# X = df.iloc[0:100, [0, 2]].values\n",
    "\n",
    "# plt.scatter(X[:50, 0], X[:50, 1], color=\"red\", marker=\"o\", label=\"setosa\")\n",
    "# plt.scatter(X[50:100, 0], X[50:100, 1], color=\"blue\", marker=\"x\", label=\"versicolor\")\n",
    "# plt.xlabel('x')\n",
    "# plt.ylabel('y')\n",
    "# plt.show()\n",
    "# y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Iris-setosa</th>\n",
       "      <th>Iris-versicolor</th>\n",
       "      <th>Iris-virginica</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Iris-setosa  Iris-versicolor  Iris-virginica\n",
       "0              1                0               0\n",
       "1              1                0               0\n",
       "2              1                0               0\n",
       "3              1                0               0\n",
       "4              1                0               0\n",
       "..           ...              ...             ...\n",
       "145            0                0               1\n",
       "146            0                0               1\n",
       "147            0                0               1\n",
       "148            0                0               1\n",
       "149            0                0               1\n",
       "\n",
       "[150 rows x 3 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y = pd.get_dummies(y, sparse=False)\n",
    "Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from sklearn.preprocessing import OneHotEncoder, LabelEncoder\n",
    "# onehotencoder = OneHotEncoder()\n",
    "# le = LabelEncoder()\n",
    "# y_le = le.fit_transform(y)\n",
    "# y_le = y_le.reshape([-1, 1])\n",
    "# Y = onehotencoder.fit_transform(y_le)\n",
    "\n",
    "# print(Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=33)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def softmax(v):\n",
    "    b = np.exp(v)\n",
    "    s = sum(b)\n",
    "    c = b/s\n",
    "    return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sigmoid(v):\n",
    "    return 1 / (1 + np.exp(-v))\n",
    "#     return np.tanh(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "w = np.array([[1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0]])\n",
    "learning_rate = 0.1;\n",
    "for n in range(0, 300):\n",
    "    for i in range(len(X_train)):\n",
    "#         随机梯度下降w\n",
    "        a = np.sum(X_train[i] * w, axis=1)\n",
    "#         print(a)\n",
    "        y_hat = softmax(a)\n",
    "\n",
    "        yy = y_train.values[i]\n",
    "        \n",
    "        t = y_hat - yy\n",
    "        t = t.reshape([-1, 1])\n",
    "        delta = (np.multiply(t, X_train[i]) + 0.0001 * w) * learning_rate\n",
    "        w -= delta\n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "w2 = np.array([[1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0]])\n",
    "learning_rate = 0.1;\n",
    "for i in range(500):\n",
    "#     批量\n",
    "    wt = X_train.reshape(100, 1, 5) * w2\n",
    "    a = np.sum(wt, axis=2)\n",
    "#     a\n",
    "\n",
    "    y_hat = sigmoid(a)\n",
    "    t = y_hat - y_train.values\n",
    "    t = t.reshape(100,-1,1)\n",
    "    err = t * X_train.reshape(100, 1, 5)\n",
    "    err = sum(err) / 100\n",
    "\n",
    "    w2 -= err * 0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  4.25473152   7.92239149 -14.55236557  -5.65182619   3.43661937]\n",
      " [  3.21242112   1.33874777  -4.9668558  -12.48708414   7.59207063]\n",
      " [ -7.90909995  -5.4720837    7.52041121   9.53335808  -5.11326269]]\n",
      "[[ 0.11202871  1.82399197 -2.49767822 -0.43686986  1.05480089]\n",
      " [-0.07833006 -0.96176117  0.49072571 -0.43694277  1.25747597]\n",
      " [-1.688391   -1.43495818  2.21797114  2.27861118 -0.11091813]]\n"
     ]
    }
   ],
   "source": [
    "print(w)\n",
    "print(w2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.98674271 0.99999995 0.99999987] [0 0 1]\n",
      "[0.99201125 1.         1.        ] [0 0 1]\n",
      "0.96\n"
     ]
    }
   ],
   "source": [
    "right_count = 0\n",
    "for i in range(len(X_test)):\n",
    "    test_a = np.sum(X_test[i] * w, axis=1)\n",
    "    y_test_ = sigmoid(test_a)\n",
    "#     y_test_ = np.sum(active(X_test[i] * w), axis=1)\n",
    "    i1 = np.argmax(y_test_)\n",
    "    i2 = np.argmax(y_test.values[i])\n",
    "#     print(i1,i2,y_test_, y_test.values[i])\n",
    "    if i1 == i2:\n",
    "        right_count = right_count+1\n",
    "    else:\n",
    "        print(y_test_, y_test.values[i])\n",
    "\n",
    "print(right_count/len(X_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[            inf 3.81757458e-152 1.51687482e-157]\n",
      "inf\n",
      "[nan  0.  0.]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\develop\\python37\\lib\\site-packages\\ipykernel_launcher.py:15: RuntimeWarning: overflow encountered in exp\n",
      "  from ipykernel import kernelapp as app\n",
      "d:\\develop\\python37\\lib\\site-packages\\ipykernel_launcher.py:19: RuntimeWarning: invalid value encountered in true_divide\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# a = np.array([1,2,3])\n",
    "# b = np.array([4,5,6])\n",
    "# print(a*b)\n",
    "# print(np.multiply(a, b))\n",
    "\n",
    "# a = a.reshape([-1, 1])\n",
    "# print(np.multiply(a, b))\n",
    "\n",
    "# print(np.power(-1,np.array([0.001,1,2])))\n",
    "\n",
    "\n",
    "\n",
    "a = np.array([773.94252626, -348.65331884, -361.08920742])\n",
    "b = np.exp(a)\n",
    "print(b)\n",
    "s = sum(b)\n",
    "print(s)\n",
    "c = b/s\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.5\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[-1.46385011, -0.87831007, -0.29277002],\n",
       "       [ 0.29277002,  0.87831007,  1.46385011]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_std = np.array([[1,2,3],[4,5,6]])\n",
    "print(X_std.mean())\n",
    "X_std = (X_std - X_std.mean()) / X_std.std()\n",
    "\n",
    "X_std"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
